package com.jxin.oms.order.domain.inorder.converter;

import com.jxin.oms.order.domain.inorder.dmo.entity.InOrder;
import com.jxin.oms.order.domain.inorder.dmo.vao.BuyerInfoVao;
import com.jxin.oms.order.domain.inorder.dmo.vao.OtherValue;
import com.jxin.oms.order.domain.inorder.repository.table.InOrderDo;
import com.jxin.oms.order.web.dto.req.InOrderSaveReqDto;
import com.jxin.util.serialize.SerializeUtil;
import org.apache.commons.lang3.ArrayUtils;
import org.springframework.util.Assert;

/**
 * InOrder 转换器
 * @author  Jxin
 * @version 1.0
 * @since   2020-01-20 16:51:14
 */
public class InOrderConverter {
    private InOrderConverter(){}
    /**
     * Convert InOrderDo to InOrder
     * @param inOrderDo 原始单Do
     * @return {@link InOrder}
     * @author Jxin
     */
    public static InOrder convertToInOrder(InOrderDo inOrderDo) {
        Assert.notNull(inOrderDo, "inOrderDo不能为null");
        final InOrder result = new InOrder();

        result.setInOrderId(inOrderDo.getInOrderId());
        result.setOrderStatus(inOrderDo.getOrderStatus());
        result.setReturnStatus(inOrderDo.getReturnStatus());
        result.setSource(inOrderDo.getSource());
        result.setType(inOrderDo.getType());
        result.setActivity(inOrderDo.getActivity());
        result.setErrType(inOrderDo.getErrType());
        result.setErrMsg(inOrderDo.getErrMsg());
        result.setPayTime(inOrderDo.getPayTime());
        result.setSendTime(inOrderDo.getSendTime());
        result.setReceivingTime(inOrderDo.getReceivingTime());
        result.setModifyTime(inOrderDo.getModifyTime());
        result.setCreateTime(inOrderDo.getCreateTime());

        final byte[] otherValueBytes = inOrderDo.getOtherValueBytes();
        if(ArrayUtils.isNotEmpty(otherValueBytes)){
            result.setOtherValue(SerializeUtil.deserialize(otherValueBytes, OtherValue.class));
        }

        final byte[] buyerInfoBytes = inOrderDo.getBuyerInfoBytes();
        if(ArrayUtils.isNotEmpty(buyerInfoBytes)){
            result.setBuyerInfoVao(SerializeUtil.deserialize(inOrderDo.getBuyerInfoBytes(), BuyerInfoVao.class));
        }

        result.setIsDeleted(inOrderDo.getIsDeleted());
        return result;
    }
    /**
     * Convert inOrderSaveReqDto to InOrder
     * @param inOrderSaveReqDto 原始单落盘 请求参数
     * @return {@link InOrder}
     * @author Jxin
     */
    public static InOrder convertToInOrder(InOrderSaveReqDto inOrderSaveReqDto) {
        Assert.notNull(inOrderSaveReqDto, "inOrderSaveReqDto不能为null");
        final InOrder result = new InOrder();

        result.setInOrderId(inOrderSaveReqDto.getInOrderId());
        result.setOrderStatus(inOrderSaveReqDto.getOrderStatus());
        result.setReturnStatus(inOrderSaveReqDto.getReturnStatus());
        result.setSource(inOrderSaveReqDto.getSource());
        result.setType(inOrderSaveReqDto.getType());
        result.setActivity(inOrderSaveReqDto.getActivity());
        result.setPayTime(inOrderSaveReqDto.getPayTime());
        // 赋值购买者信息
        result.setBuyerInfoVao(BuyerInfoVaoConverter.convertToBuyerInfoVao(inOrderSaveReqDto));
        // 赋值其他值对象
        result.setOtherValue(OtherValue.newOfValueMap(inOrderSaveReqDto.getOtherValue()));

        return result;
    }
    /**
     * Convert InOrder to InOrderDo
     * @param inOrder 原始单Dmo
     * @return {@link InOrderDo}
     * @author Jxin     
     */
    public static InOrderDo convertToInOrderDo(InOrder inOrder) {
        Assert.notNull(inOrder, "inOrder不能为null");
        final InOrderDo result = new InOrderDo();

        result.setInOrderId(inOrder.getInOrderId());
        result.setOrderStatus(inOrder.getOrderStatus());
        result.setReturnStatus(inOrder.getReturnStatus());
        result.setSource(inOrder.getSource());
        result.setType(inOrder.getType());
        result.setActivity(inOrder.getActivity());
        result.setErrType(inOrder.getErrType());
        result.setErrMsg(inOrder.getErrMsg());
        result.setPayTime(inOrder.getPayTime());
        result.setSendTime(inOrder.getSendTime());
        result.setReceivingTime(inOrder.getReceivingTime());
        result.setModifyTime(inOrder.getModifyTime());
        result.setCreateTime(inOrder.getCreateTime());

        result.setBuyerInfoBytes(SerializeUtil.serialize(inOrder.getBuyerInfoVao()));
        result.setOtherValueBytes(SerializeUtil.serialize(inOrder.getOtherValue()));

        result.setIsDeleted(inOrder.getIsDeleted());
        return result;
    }
}
